package com.monlyu.bo;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.monlyu.bean.Page;
import com.monlyu.bean.Pages;
import com.monlyu.util.DateUtil;
import com.monlyu.util.StringUtil;
import com.monlyu.vo.Manuscript;

/********************************************************************************
 * 
 * <b>功能描述☞</b><br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.bo.OuterBo.java<br>
 * <b>开发日期☞</b>2010-2-12 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
@Repository(value = "voiceBo")
public class VoiceBo extends BaseBo {

	public Pages<Manuscript> findManuscript(Manuscript manuscript, Date beginDate, Date endDate, Page page) {
		Criteria criteria = dao.initCriteria(Manuscript.class);
		criteria.add(Restrictions.eq("adv", false));
		criteria.add(Restrictions.eq("auditZt", Manuscript.STATUS_DONE));
		if (manuscript!=null && !StringUtil.isEmpty(manuscript.getTitle())) {
			criteria.add(Restrictions.like("title", manuscript.getTitle().trim(), MatchMode.ANYWHERE));
		}
		if (beginDate != null) {
			criteria.add(Restrictions.ge("createDate", beginDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.lt("createDate", DateUtil.addDay(endDate, 1)));
		}
		if (manuscript!=null && manuscript.getColumn() > 0) {
			criteria.add(Restrictions.eq("column", manuscript.getColumn()));
		}
		if (manuscript!=null && !StringUtil.isEmpty(manuscript.getUser().getId())) {
			criteria.createAlias("user", "user");
			criteria.add(Restrictions.eq("user.id", manuscript.getUser().getId()));
		}
		criteria.addOrder(Order.desc("createDate"));
		return buildPages(page, criteria);
	}

	/**
	 * 添加配音
	 */
	public void addVoice(Manuscript manuscript) {
		String hql = "update com.monlyu.vo.Manuscript m set m.timeLong=?,m.puser=? where m.id=?";
		dao.execute(hql, manuscript.getTimeLong(), curUser(), manuscript.getId());
	}

	public Object getContent(Long id) {
		Manuscript manus = dao.get(Manuscript.class, id);
		return manus.getContent();
	}
}
